﻿// -----------------------------------------------------------------------
// <copyright file="CustomerViewModel.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ModuleCustomer.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ModulesInfrastructure.ViewModels;
    using ServerConnection.ShopServiceReference;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    using Microsoft.Practices.Prism.Events;
    using EventInfrastructure;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CustomerViewModel : SharedViewModel, ICustomerViewModel
    {
        private User m_selected;

        private int m_place = -1;

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        private ObservableCollection<User> _list = new ObservableCollection<User>();

        public ObservableCollection<User> List
        {
            get { return _list; }
        }

        public Dictionary<int, string> Places
        {
            get
            {
                Dictionary<int, string> result = new Dictionary<int, string>();

                PlaceView[] list = ServerConnection.Service.Instance.GetPlaces(ServerConnection.Service.SelectedLanguage);

                result.Clear();

                if (list != null && list.Count() != 0)
                {
                    if (m_place == -1)
                    {
                        m_place = list[0].Id;
                    }

                    foreach (var item in list)
                    {
                        if (ServerConnection.Service.PlaceId == 0 || item.Id == ServerConnection.Service.PlaceId)
                        {
                            result.Add(item.Id, item.Name);
                        }
                    }
                }

                return result;
            }
        }

        private void Refresh()
        {
            Dictionary<string, object> filter = new Dictionary<string, object>();
            filter.Add("place", this.m_place);
            Load(filter);
        }

        public void Load(Dictionary<string, object> filter)
        {
            Dictionary<long, User> list = ServerConnection.Service.Instance.GetUserList(filter, 0);

            _list.Clear();

            foreach (User item in list.Values)
            {
                _list.Add(item);
            }

            this.PropertyChanged(this, new PropertyChangedEventArgs("List"));
        }


        public User OnSelected
        {
            get
            {
                return m_selected;
            }
            set
            {
                m_selected = value;
            }
        }

        public object OnSelectedPlace
        {
            get
            {
                return m_place;
            }
            set
            {
                m_place = ((KeyValuePair<int, string>)value).Key;
                this.Refresh();
            }
        }

    }
}
